from django.shortcuts import HttpResponse,render,redirect
import pymysql
from django.shortcuts import render
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
#导入render和HttpResponse模块
from django.shortcuts import render,HttpResponse

#导入Paginator,EmptyPage和PageNotAnInteger模块
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

#从Django项目的应用中导入模块
# from app01.models import *

def main(request):
    i=1
    return render (request, 'main.html',{'i':1})

def main2(request):
    i=1
    return render (request, 'main2.html',{'i':1})

def ranking(request):
    i=1
    return render (request, 'ranking list.html',{'i':1})

def ranking_click(request):
    try:
        if request.method=='POST':
            sid=request.POST.get('sid')
            sid=int(sid)
            # print(sid)
            list1=[]
            db = pymysql.connect(host="localhost", port=3307, user="root", password="1234", db="fruitdb",
                                 charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql = "select title,img,authorname,kind,state,intro,clickcount from ranking order by clickcount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen=len(result)
            if len(result) % 5 == 0:
                length = len(result) // 5
            else:
                length = len(result) // 5 + 1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid < 1:
                sid = 1
            elif sid >len(list1):
                sid=len(list1)
            if sid - 2 > 1 and sid + 2 < len(list1):
                z = 3
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 == 1 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 > 1 and sid + 2 == len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid - 2 > 1 and sid + 2 > len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            # print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            return render(request, 'ranking_click.html', {'alllen':alllen, 'result': result, 'list1': list1, 'sid': sid, 'length': length, 'z': z, 'm': m, 'n': n})
        elif request.method=='GET':
            sid= request.GET.get('sid')
            a=sid.split(',')
            sid=int(a[0])
            b=a[1].split('=')
            snum=int(b[1])
            # print(sid,snum)
            sid=sid+snum
            sid=int(sid)
            list1 = []
            db = pymysql.connect(host="localhost",port=3307,user="root",password="1234", db="fruitdb",charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql="select title,img,authorname,kind,state,intro,clickcount from ranking order by clickcount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen=len(result)
            if len(result) % 5 ==0:
                length = len(result) // 5
            else:
                length = len(result) // 5 +1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid -2 >1 and sid + 2 < len(list1):
                z=3
                m=1
                n=len(list1)
                list1 = list1[sid-3:sid + 2]
            elif sid -2 ==1 and sid + 2 < len(list1):
                z=1
                m=1
                n=len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid -2 > 1 and sid + 2 == len(list1):
                z=2
                m=1
                n=len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid -2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid -2 >1 and sid+2>len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid-3:sid + 2]
            print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            # print(result)
            return render(request, 'ranking_click.html', {'alllen':alllen, 'result': result, 'list1': list1, 'sid':sid, 'length':length, 'z': z, 'm':m, 'n':n})
    except:
        sid=1
        list1=[]
        db=pymysql.connect(host="localhost",port=3307,user="root",password="1234", db="fruitdb",charset='utf8')
        cursor = db.cursor(pymysql.cursors.DictCursor)
        sql='select title,img,authorname,kind,state,intro,clickcount from ranking order by clickcount desc'
        cursor.execute(sql)
        result = cursor.fetchall()
        alllen=len(result)
        if len(result) % 5 ==0:
            length = len(result) // 5
        else:
            length = len(result) // 5 +1
        for x in range(1,length+1):
            {
               list1.append(x)
            }
        # print(list1)
        a=5*(sid-1)
        b=5*sid
        # print(a,b)
        result=result[a:b]
        # print(result)
        z = 1
        m = 1
        n = len(list1)
        list1 = list1[0:3]
        return render (request, 'ranking_click.html', {'alllen':alllen, 'result':result, 'list1':list1, 'sid':sid, 'z': z, 'm':m, 'n':n})

def ranking_ticket(request):
    try:
        if request.method=='POST':
            sid=request.POST.get('sid')
            sid=int(sid)
            # print(sid)
            list1=[]
            db = pymysql.connect(host="localhost", port=3307, user="root", password="1234", db="fruitdb",
                                 charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql = "select title,img,authorname,kind,state,intro,ticketcount from ranking order by ticketcount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen=len(result)
            if len(result) % 5 == 0:
                length = len(result) // 5
            else:
                length = len(result) // 5 + 1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid < 1:
                sid = 1
            elif sid >len(list1):
                sid=len(list1)
            if sid - 2 > 1 and sid + 2 < len(list1):
                z = 3
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 == 1 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 > 1 and sid + 2 == len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid - 2 > 1 and sid + 2 > len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            # print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            return render(request, 'ranking_ticket.html', {'alllen':alllen, 'result': result, 'list1': list1, 'sid': sid, 'length': length, 'z': z, 'm': m, 'n': n})
        elif request.method=='GET':
            sid= request.GET.get('sid')
            a=sid.split(',')
            sid=int(a[0])
            b=a[1].split('=')
            snum=int(b[1])
            # print(sid,snum)
            sid=sid+snum
            sid=int(sid)
            list1 = []
            db = pymysql.connect(host="localhost",port=3307,user="root",password="1234", db="fruitdb",charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql="select title,img,authorname,kind,state,intro,ticketcount from ranking  order by ticketcount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen=len(result)
            if len(result) % 5 ==0:
                length = len(result) // 5
            else:
                length = len(result) // 5 +1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid -2 >1 and sid + 2 < len(list1):
                z=3
                m=1
                n=len(list1)
                list1 = list1[sid-3:sid + 2]
            elif sid -2 ==1 and sid + 2 < len(list1):
                z=1
                m=1
                n=len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid -2 > 1 and sid + 2 == len(list1):
                z=2
                m=1
                n=len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid -2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid -2 >1 and sid+2>len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid-3:sid + 2]
            print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            # print(result)
            return render(request, 'ranking_ticket.html', {'alllen':alllen, 'result': result, 'list1': list1, 'sid':sid, 'length':length, 'z': z, 'm':m, 'n':n})
    except:
        sid=1
        list1=[]
        db=pymysql.connect(host="localhost",port=3307,user="root",password="1234", db="fruitdb",charset='utf8')
        cursor = db.cursor(pymysql.cursors.DictCursor)
        sql='select title,img,authorname,kind,state,intro,ticketcount from ranking order by ticketcount desc'
        cursor.execute(sql)
        result = cursor.fetchall()
        alllen=len(result)
        if len(result) % 5 ==0:
            length = len(result) // 5
        else:
            length = len(result) // 5 +1
        for x in range(1,length+1):
            {
               list1.append(x)
            }
        # print(list1)
        a=5*(sid-1)
        b=5*sid
        # print(a,b)
        result=result[a:b]
        # print(result)
        z = 1
        m = 1
        n = len(list1)
        list1 = list1[0:3]
        return render (request, 'ranking_ticket.html', {'alllen':alllen, 'result':result, 'list1':list1, 'sid':sid, 'z': z, 'm':m, 'n':n})


def ranking_sell(request):
    try:
        if request.method=='POST':
            sid=request.POST.get('sid')
            sid=int(sid)
            # print(sid)
            list1=[]
            db = pymysql.connect(host="localhost", port=3307, user="root", password="1234", db="fruitdb",
                                 charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql = "select title,img,authorname,kind,state,intro,sellcount from ranking order by sellcount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen=len(result)
            if len(result) % 5 == 0:
                length = len(result) // 5
            else:
                length = len(result) // 5 + 1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid < 1:
                sid = 1
            elif sid >len(list1):
                sid=len(list1)
            if sid - 2 > 1 and sid + 2 < len(list1):
                z = 3
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 == 1 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 > 1 and sid + 2 == len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid - 2 > 1 and sid + 2 > len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            # print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            return render(request, 'ranking_sell.html', {'alllen':alllen, 'result': result, 'list1': list1, 'sid': sid, 'length': length, 'z': z, 'm': m, 'n': n})
        elif request.method=='GET':
            sid= request.GET.get('sid')
            a=sid.split(',')
            sid=int(a[0])
            b=a[1].split('=')
            snum=int(b[1])
            # print(sid,snum)
            sid=sid+snum
            sid=int(sid)
            list1 = []
            db = pymysql.connect(host="localhost",port=3307,user="root",password="1234", db="fruitdb",charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql="select title,img,authorname,kind,state,intro,sellcount from ranking  order by sellcount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen=len(result)
            if len(result) % 5 ==0:
                length = len(result) // 5
            else:
                length = len(result) // 5 +1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid -2 >1 and sid + 2 < len(list1):
                z=3
                m=1
                n=len(list1)
                list1 = list1[sid-3:sid + 2]
            elif sid -2 ==1 and sid + 2 < len(list1):
                z=1
                m=1
                n=len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid -2 > 1 and sid + 2 == len(list1):
                z=2
                m=1
                n=len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid -2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid -2 >1 and sid+2>len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid-3:sid + 2]
            print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            # print(result)
            return render(request, 'ranking_sell.html', {'alllen':alllen, 'result': result, 'list1': list1, 'sid':sid, 'length':length, 'z': z, 'm':m, 'n':n})
    except:
        sid=1
        list1=[]
        db=pymysql.connect(host="localhost",port=3307,user="root",password="1234", db="fruitdb",charset='utf8')
        cursor = db.cursor(pymysql.cursors.DictCursor)
        sql='select title,img,authorname,kind,state,intro,sellcount from ranking order by sellcount desc'
        cursor.execute(sql)
        result = cursor.fetchall()
        alllen=len(result)
        if len(result) % 5 ==0:
            length = len(result) // 5
        else:
            length = len(result) // 5 +1
        for x in range(1,length+1):
            {
               list1.append(x)
            }
        # print(list1)
        a=5*(sid-1)
        b=5*sid
        # print(a,b)
        result=result[a:b]
        # print(result)
        z = 1
        m = 1
        n = len(list1)
        list1 = list1[0:3]
        return render (request, 'ranking_sell.html', {'alllen':alllen, 'result':result, 'list1':list1, 'sid':sid, 'z': z, 'm':m, 'n':n})


def ranking_len(request):
    try:
        if request.method == 'POST':
            sid = request.POST.get('sid')
            sid = int(sid)
            # print(sid)
            list1 = []
            db = pymysql.connect(host="localhost", port=3307, user="root", password="1234", db="fruitdb",
                                 charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql = "select title,img,authorname,kind,state,intro,lencount from ranking order by lencount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen = len(result)
            if len(result) % 5 == 0:
                length = len(result) // 5
            else:
                length = len(result) // 5 + 1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid < 1:
                sid = 1
            elif sid > len(list1):
                sid = len(list1)
            if sid - 2 > 1 and sid + 2 < len(list1):
                z = 3
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 == 1 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 > 1 and sid + 2 == len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid - 2 > 1 and sid + 2 > len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            # print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            return render(request, 'ranking_len.html',
                          {'alllen': alllen, 'result': result, 'list1': list1, 'sid': sid, 'length': length, 'z': z,
                           'm': m, 'n': n})
        elif request.method == 'GET':
            sid = request.GET.get('sid')
            a = sid.split(',')
            sid = int(a[0])
            b = a[1].split('=')
            snum = int(b[1])
            # print(sid,snum)
            sid = sid + snum
            sid = int(sid)
            list1 = []
            db = pymysql.connect(host="localhost", port=3307, user="root", password="1234", db="fruitdb",
                                 charset='utf8')
            cursor = db.cursor(pymysql.cursors.DictCursor)
            sql = "select title,img,authorname,kind,state,intro,lencount from ranking  order by lencount desc"
            cursor.execute(sql)
            result = cursor.fetchall()
            alllen = len(result)
            if len(result) % 5 == 0:
                length = len(result) // 5
            else:
                length = len(result) // 5 + 1
            # print(length)
            for x in range(1, length + 1):
                {
                    list1.append(x)
                }
            z = 1
            m = 1
            n = len(list1)
            if sid - 2 > 1 and sid + 2 < len(list1):
                z = 3
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 == 1 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 > 1 and sid + 2 == len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            elif sid - 2 <= 0 and sid + 2 < len(list1):
                z = 1
                m = 1
                n = len(list1)
                list1 = list1[0:sid + 2]
            elif sid - 2 > 1 and sid + 2 > len(list1):
                z = 2
                m = 1
                n = len(list1)
                list1 = list1[sid - 3:sid + 2]
            print(list1)
            a = 5 * (sid - 1)
            b = 5 * sid
            # print(a, b)
            result = result[a:b]
            # print(result)
            return render(request, 'ranking_len.html',
                          {'alllen': alllen, 'result': result, 'list1': list1, 'sid': sid, 'length': length, 'z': z,
                           'm': m, 'n': n})
    except:
        sid = 1
        list1 = []
        db = pymysql.connect(host="localhost", port=3307, user="root", password="1234", db="fruitdb", charset='utf8')
        cursor = db.cursor(pymysql.cursors.DictCursor)
        sql = 'select title,img,authorname,kind,state,intro,lencount from ranking order by lencount desc'
        cursor.execute(sql)
        result = cursor.fetchall()
        alllen = len(result)
        if len(result) % 5 == 0:
            length = len(result) // 5
        else:
            length = len(result) // 5 + 1
        for x in range(1, length + 1):
            {
                list1.append(x)
            }
        # print(list1)
        a = 5 * (sid - 1)
        b = 5 * sid
        # print(a,b)
        result = result[a:b]
        # print(result)
        z = 1
        m = 1
        n = len(list1)
        list1 = list1[0:3]
        return render(request, 'ranking_len.html',{'alllen': alllen, 'result': result, 'list1': list1, 'sid': sid, 'z': z, 'm': m, 'n': n})

def search(request):
    i=1
    return render (request, 'search.html', {'i':1})
#
# def timefree(request):
#     i=1
#     return render (request, 'timefree.html',{'i':1})


# def index(request):
#     # 获取Book数据表中的所有记录
#     book_list = [1,2,3,4,5,6,7,8,9,0]
#
#     # 生成paginator对象,定义每页显示10条记录
#     paginator = Paginator(book_list, 10)
#
#     # 从前端获取当前的页码数,默认为1
#     page = request.GET.get('page', 1)
#
#     # 把当前的页码数转换成整数类型
#     currentPage = int(page)
#
#     try:
#         print(page)
#         book_list = paginator.page(page)  # 获取当前页码的记录
#     except PageNotAnInteger:
#         book_list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
#     except EmptyPage:
#         book_list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
#
#     return render(request, "index.html", locals())

def self(request):
    i=1
    v=request.POST.get('i')
    return render (request, 'self.html', {'i':v})

# def a(request):
#     if request.method=='GET':
#         img=1
#         return render(request, '1.html', {'img': img})
#     if request.method=='POST':
#         myPhoto = request.POST.get('myPhoto')
#         return render(request,'1.html',{'img':myPhoto})
# def c(request):
#     if request.method=='GET':
#         img=1
#         return render(request, '1.html', {'img': img})
#     if request.method=='POST':
#         file= request.POST.get('file')
#         return render(request,'1.html',{'file':file})

def selfset(request):
    i=1
    return render (request, 'selfset.html', {'i':1})